package Servlet;

import com.cyy.bean.record;
import com.cyy.bean.record;
import com.cyy.dao.*;
import com.cyy.dao.recordDao;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.sql.SQLException;
import java.util.ArrayList;

@WebServlet({"/showCart","/showHistory","/showCollect"})
public class showServelt extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("UTF-8");
        String userName=request.getSession().getAttribute("name").toString();
        ArrayList<record> records=null;
        String uri=request.getRequestURI();
        try {
//            判断具体是哪个url，type1=0代表待发货
            if(uri.equals("/showCart")) {
                cartDao cartDao=new cartDao();
//                向上转型
                records = cartDao.selectAll(userName,0);
            }
            else if(uri.equals("/showCollect")) {
                collectDao collectDao=new collectDao();
                records = collectDao.selectAll(userName,0);
                request.setAttribute("message",request.getAttribute("message"));
            }
            else if(uri.equals("/showHistory")){
                historyDao historyDao=new historyDao();
                records = historyDao.selectAll(userName,0);
                request.setAttribute("message",request.getAttribute("message"));
            }
        } catch (SQLException | ClassNotFoundException throwables) {
            throwables.printStackTrace();
        }
        int totalPrice=0,totalNum=0;
        if(records==null)
        {
            System.out.println("结果集为空！");
        }
        else {
            if(uri.equals("/showCart")) {
                for (int i = 0; i < records.size(); i++) {
                    if(records.get(i).getUsability()==1) {
                        totalPrice += records.get(i).getPrice() * records.get(i).getNum();
                        totalNum += records.get(i).getNum();
                    }
                }
            }
        }
        if(uri.equals("/showCart"))
        {
            request.setAttribute("totalPrice", totalPrice);
            request.setAttribute("totalNum", totalNum);
            request.setAttribute("carts", records);
            request.getRequestDispatcher("/customerCart.jsp").forward(request, response);
        }
        else if(uri.equals("/showCollect")) {
            request.setAttribute("collects",records);
            request.getRequestDispatcher("/customerCollect.jsp").forward(request,response);
        }
        else
        {
            request.setAttribute("history", records);
            request.getRequestDispatcher("/customerHistory.jsp").forward(request, response);
        }
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.doGet(request,response);
    }
}
